package com.qf.android.huihui.presenter;

import android.util.Log;

import com.qf.android.huihui.adapter.shopping.ShoppingAdapter;
import com.qf.android.huihui.bean.ShoppingBean;
import com.qf.android.huihui.bean.show.shoppingbottom.CommonBean;
import com.qf.android.huihui.bean.show.shoppingbottom.Hot;
import com.qf.android.huihui.bean.show.shoppingbottom.HotBusiness;
import com.qf.android.huihui.bean.show.shoppingbottom.ShoppingBase;
import com.qf.android.huihui.bean.show.shoppingbottom.ShoppingBottomBean;
import com.qf.android.huihui.common.InfoRecyclerView;
import com.qf.android.huihui.model.ShoppingModel;
import com.qf.android.huihui.model.impl.IShopping;
import com.qf.android.huihui.presenter.impl.BasePresenter;
import com.qf.android.huihui.view.fragment.impl.IShowView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func2;
import rx.functions.Func4;
import rx.schedulers.Schedulers;

/**
 * Created by lx on 2017/1/6.
 */

public class ShoppingPresenter implements BasePresenter {
    private IShopping iShopping;
    private IShowView iShowView;
    public ShoppingPresenter( IShowView iShowView) {
        this.iShopping = new ShoppingModel();
        this.iShowView = iShowView;
    }

    @Override
    public void getData() {

    }

    @Override
    public void getData(Object page, Object type, final boolean isUpfresh, final boolean isDownfresh, final Object adapter, final Object recyclerview) {

            Observable<CommonBean> commit = Observable.zip(
                    iShopping.loadHotBusiness(),
                    iShopping.loadHot(),
                    iShopping.loadInfo(),
                    iShopping.loadShoppingBottomBean(),
                    new Func4<HotBusiness, Hot, ShoppingBean, ShoppingBottomBean, CommonBean>() {

                        @Override
                        public CommonBean call(HotBusiness hotBusiness, Hot hot, ShoppingBean shoppingBean, ShoppingBottomBean shoppingBottomBean) {
                            return new CommonBean(hotBusiness, hot, shoppingBean, shoppingBottomBean);
                        }
                    });
            commit.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<CommonBean>() {
                        @Override
                        public void onCompleted() {
                            if(adapter != null && recyclerview != null){
                                ((InfoRecyclerView)recyclerview).onRefreshComplete();
                                ((ShoppingAdapter)adapter).notifyDataSetChanged();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            Log.e("sss", e.getMessage());
                        }

                        @Override
                        public void onNext(CommonBean commonBean) {
                            iShowView.setData(commonBean.getList());
                        }
                    });


    }
    //shangla
    @Override
    public void getData(final Object adapter, final Object recyclerview) {
        Observable<CommonBean> commit =Observable.zip(
                iShopping.loadShoppingBottomBean()
                ,iShopping.loadHot()
                ,new Func2<ShoppingBottomBean,Hot,CommonBean>() {


                    @Override
                    public CommonBean call(ShoppingBottomBean shoppingBottomBean, Hot hot) {
                        return new CommonBean(hot,shoppingBottomBean);
                    }
                });
        commit.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CommonBean>() {
                    @Override
                    public void onCompleted() {
                        ((InfoRecyclerView)recyclerview).onRefreshComplete();
                        ((ShoppingAdapter)adapter).notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CommonBean commonBean) {
                        List<ShoppingBase> list=new ArrayList<ShoppingBase>();
                        list=commonBean.getData(list);
                        iShowView.setData(list);
                    }
                });
    }


}
